package com.info3k.server.domain;

import java.io.Serializable;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.info3k.client.domain.Category;


@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class CategoryP implements Serializable {

	private static final long serialVersionUID = -1873799364997737422L;

	@PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    private Key id;
    
    @Persistent
	private String code;
    
    @Persistent
    private String name;
    
    public CategoryP() {
    	
    }
    
    public CategoryP(Category category) {
    	this.setCode(category.getCode());
    	this.setName(category.getName());
    	if(category.getId()!=null)
    		this.setId(KeyFactory.createKey(CategoryP.class.getSimpleName(), category.getId()));
    	else 
    		this.setId(KeyFactory.createKey(CategoryP.class.getSimpleName(), System.currentTimeMillis()));
    }
    
    public static Category createCategory(CategoryP categoryP){
		Category category = new Category();
		category.setId(categoryP.getId().getId());
		category.setCode(categoryP.getCode());
		category.setName(categoryP.getName());
		return category;
    }

	public void setId(Key id) {
		this.id = id;
	}

	public Key getId() {
		return id;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getCode() {
		return code;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getName() {
		return name;
	}
}
